package com.zksk.admin.controller.portal;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksk.admin.api.domain.portal.dto.ManualDto;
import com.zksk.admin.api.domain.portal.vo.ManualVo;
import com.zksk.admin.api.domain.portal.vo.PolicyInfoVo;
import com.zksk.admin.service.portal.ManualService;
import com.zksk.admin.service.portal.PolicyInfoService;
import com.zksk.common.core.constant.PolicyInfoConstants;
import com.zksk.common.core.constant.PortalConstants;
import com.zksk.common.core.domain.R;
import com.zksk.common.core.utils.StringUtils;
import com.zksk.common.core.web.controller.BaseController;
import com.zksk.common.log.annotation.Log;
import com.zksk.common.log.enums.BusinessType;
import com.zksk.common.security.utils.SecurityUtils;
import com.zksk.mbg.admin.domain.Manual;
import com.zksk.mbg.admin.domain.PolicyInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description:
 * @author: xiebo
 * @create: 2022-12-28 14:20
 **/
@Slf4j
@RestController
@RequestMapping("/manual/")
@Api(value = "ManualController", tags = "操作指南相关接口")
public class ManualController extends BaseController
{

    @Autowired
    private ManualService manualService;

    /**
     * 分页条件查询操作指南
     *
     * @param manualDto
     * @return
     */
    @Log(title = "查询操作指南", businessType = BusinessType.SELECT)
    @PostMapping("list")
    @ApiOperation(value = "分页条件查询操作指南")
    public R<Page<ManualVo>> list(@RequestBody ManualDto manualDto)
    {

        Page<ManualVo> voPage = new Page<>();
        Page<Manual> page = manualService.page(
                //分页
                new Page<>(manualDto.getPageNum(), manualDto.getPageSize()),
                //条件构造
                new LambdaQueryWrapper<Manual>()
                        .eq(StrUtil.isNotEmpty(manualDto.getType()), Manual::getType, manualDto.getType())
                        .like(StrUtil.isNotEmpty(manualDto.getTitleName()), Manual::getTitleName, manualDto.getTitleName())
        );
        //转vo
        BeanUtil.copyProperties(page, voPage);
        return R.ok(voPage);

    }

    /**
     * 根据操作指南id查询操作指南
     *
     * @param id
     * @return
     */
    @Log(title = "根据操作指南id查询操作指南", businessType = BusinessType.SELECT)
    @GetMapping(value = "getById/{id}")
    @ApiOperation(value = "根据操作指南id查询操作指南")
    public R<ManualVo> getById(@PathVariable Long id)
    {
        Manual manual = manualService.getById(id);
        if (ObjectUtil.isEmpty(manual.getLookNum()))
        {
            manual.setLookNum(PortalConstants.LOOK_NUM_1);
        }
        else
        {
            manual.setLookNum(manual.getLookNum() + 1);
        }
        manualService.updateById(manual);
        return R.ok(BeanUtil.copyProperties(manual,ManualVo.class));
    }

    /**
     * 删除资讯信息
     *
     * @param ids
     * @return
     */
    @Log(title = "删除操作指南", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{ids}")
    @ApiOperation(value = "删除操作指南(逻辑删除)")
    public R<String> remove(@PathVariable List<Long> ids)
    {
        manualService.removeBatchByIds(ids);
        return R.ok("删除成功");
    }


    /**
     * 新增资讯信息
     *
     * @param manualVo
     * @return
     */
    @Log(title = "新增操作指南", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增操作指南")
    public R add(@Validated(Insert.class) @RequestBody ManualVo manualVo)
    {
        Manual manual = BeanUtil.copyProperties(manualVo, Manual.class);
        manual.setCreateBy(SecurityUtils.getUsername());
        manualService.save(manual);
        return R.ok(manual.getId(), "新增成功");
    }

    /**
     * 修改操作指南
     *
     * @param manualVo
     * @return
     */
    @Log(title = "修改操作指南", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改操作指南")
    public R update(@Validated(Update.class) @RequestBody ManualVo manualVo)
    {
        Manual manual = BeanUtil.copyProperties(manualVo, Manual.class);
        manual.setUpdateBy(SecurityUtils.getUsername());
        manualService.updateById(manual);
        return R.ok("修改成功！", "修改成功！");
    }

}
